package com.chinatgl.latte.util.dimen;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.chinatgl.latte.app.Latte;

import java.util.Random;


/**
 * 常用单位转换的辅助类
 * 
 * @author zhy
 * 
 */
public class DensityUtils {


	private DensityUtils() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");
	}


	/**
	 * 把一个dp单位的值转换为px（像素）
	 * @param dp
	 * @return
	 */
	public static int dp2px(float dp) {
		float density = Latte.getApplication().getResources().getDisplayMetrics().density;	// 获取屏幕密度
		int px = (int) (dp * density + 0.5);	// 加0.5是为了四舍五入
		return px;
	}

	/**
	 * sp转px
	 * 
	 * @param context
	 * @param
	 * @return
	 */
	public static int sp2px(Context context, float spVal) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal,
				context.getResources().getDisplayMetrics());
	}

	/**
	 * px转dp
	 * 
	 * @param context
	 * @param pxVal
	 * @return
	 */
	public static float px2dp(Context context, float pxVal) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (pxVal / scale);
	}

	/**
	 * px转sp
	 * 
	 * @param
	 * @param pxVal
	 * @return
	 */
	public static float px2sp(Context context, float pxVal) {
		return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
	}

	/**
	 * 获得屏幕宽度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.widthPixels;
	}

	/**
	 * 获得屏幕高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.heightPixels;
	}

	/**
	 * 获得状态栏的高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getStatusHeight(Context context) {

		int statusHeight = -1;
		try {
			Class<?> clazz = Class.forName("com.android.internal.R$dimen");
			Object object = clazz.newInstance();
			int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
			statusHeight = context.getResources().getDimensionPixelSize(height);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return statusHeight;
	}

	/**
	 * 获取当前屏幕截图，包含状态栏
	 * 
	 * @param activity
	 * @return
	 */
	public static Bitmap snapShotWithStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 * 获取当前屏幕截图，不包含状态栏
	 * 
	 * @param activity
	 * @return
	 */
	public static Bitmap snapShotWithoutStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;

		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
		view.destroyDrawingCache();
		return bp;

	}



	/**
	 * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}











	/** 创建一个颜色颜色 */
	public static int createRandomColor() {
		Random random = new Random();
		int red = 50 + random.nextInt(151);// 50 ~ 200
		int green = 50 + random.nextInt(151);// 50 ~ 200
		int blue = 50 + random.nextInt(151);// 50 ~ 200
		int color = Color.rgb(red, green, blue);
		return color;
	}

	/** 创建一个带有随机颜色选择器的TextView */
	@SuppressWarnings("deprecation")
	public static TextView createRandomColorSelectorTextView(Context context) {
		final TextView textView = new TextView(context);
		textView.setTextColor(Color.WHITE);
		int padding = dp2px(6);
		textView.setPadding(padding, padding, padding, padding);
		textView.setGravity(Gravity.CENTER);
		textView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//showToast(String.valueOf(textView.getText()));
			}
		});
		textView.setBackgroundDrawable(createRandomColorSelector());
		return textView;
	}

	/** 创建一个随机颜色选择器 */
	private static Drawable createRandomColorSelector() {
		// 创建一个选择器对象
		StateListDrawable stateListDrawable = new StateListDrawable();

		// 创建一个按下状态和按下状态要显示的Drawable
		int[] pressedState = {android.R.attr.state_pressed, android.R.attr.state_enabled};
		Drawable pressedDrawable = createRandomColorDrawable();

		// 创建一个正常状态和正常状态要显示的Drawable
		int[] normalState = {};
		Drawable normalDrawable = createRandomColorDrawable();

		stateListDrawable.addState(pressedState, pressedDrawable);
		stateListDrawable.addState(normalState, normalDrawable);
		return stateListDrawable;
	}

	/** 创建一个随机颜色的圆角矩形 */
	private static Drawable createRandomColorDrawable() {
		// 创建一个图形对象
		GradientDrawable gradientDrawable = new GradientDrawable();
		gradientDrawable.setShape(GradientDrawable.RECTANGLE);// 设置图形为矩形
		gradientDrawable.setCornerRadius(dp2px(5));			// 设置矩形的圆角角度
		gradientDrawable.setColor(createRandomColor());		// 设置矩形的颜色
		return gradientDrawable;
	}

}
